<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}
$subjectid=empty($_POST['subjectid'])?0:(int)$_POST['subjectid'];
if(empty($subjectid)){
       jumpajax('300','调查主题不存在!',''); 
}
$row=$ezhand->select("vote_subject",'*','subjectid='.$subjectid);
if(empty($row)){
 jumpajax('300','调查主题不存在!',''); 
}
$today=date("Y-m-d");
if($today>$row["todate"]){
   jumpajax('300','调查主题已经过期，不能再提交!','');   
}
if($today<$row["fromdate"]){
   jumpajax('300','调查主题还未开始，请到'.$row["fromdate"].'再来参与!','');   
}
$ip=get_client_ip();
if($row["myinterval"]==0){
    
    $row=$ezhand->select("vote_data",'id','ip="'.$ip.'" and subjectid='.$subjectid);
    if(!empty($row)){
       jumpajax('300','您已经参与过此调查了，不能再次提交!','');  
    }
}else{
    $vtime=time()-$row["myinterval"]*24*3600;
    $row=$ezhand->select("vote_data",'id','ip="'.$ip.'" and subjectid='.$subjectid.' and votetime>='.$vtime);
    if(!empty($row)){
       jumpajax('300','您已经参与过此调查了，不能再次提交!','');  
    }    
    
}

$itemrow=$ezhand->select_all("vote_option_item",'*','subjectid='.$subjectid.' order by listorder asc,id asc');
$vdata=$_POST["vdata"];
if(!is_array($vdata)){
   jumpajax('300','提交的投票数据错误!','');   
}
$y=0;
while(list($k,$v)=each($itemrow)){
    $rid=$v["id"];
    if(empty($vdata[$rid])){
        $y++;
    }
}
if($y>0){
    
     jumpajax('300','投票调查项目没有填写完整，请检查!','');     
}


    $voteinfo=array(
    "subjectid"=>$subjectid,
    "voteoption"=>serialize($vdata),
    "userid"=>$_SESSION['EZ_Admin']['id'],    
    "username"=>$_SESSION['EZ_Admin']['name'],
    "votetime"=>time(),
    "utype"=>0,
    "ip"=>get_client_ip()
    );
$vid=$ezhand->insert("vote_data",$voteinfo);
while(list($itemid,$v)=each($vdata)){
   
   while(list($k,$v2)=each($v)){ 
    if(!is_numeric($v2)){
        $v2=0;
    }
    $voptioninfo=array(
    "subjectid"=>$subjectid,
    "itemid"=>$itemid,
    "vid"=>$vid,
    "optionid"=>$v2,
    );  
    $ezhand->insert("vote_option_data",$voptioninfo);
    
    } 
    
}

jumpajax('200','调查提交成功','','closeCurrent');
?>